<?php  if ( ! defined('__CORE_PATH__')) exit('No direct script access allowed');
/* SVN FILE: $Id: app_controller.php 25 2008-08-29 21:58:28Z namaless $ */
/**
 * Helper HTML.
 *
 * @author			Pereira Pulido Nuno Ricardo | Namaless | namaless@gmail.com
 * @copyright		Copyright 1981-2008, Namaless.
 * @link			http://www.namaless.com Namaless Blog
 * @version			1.0
 * @version			$Revision: 25 $
 * @modifiedby		$LastChangedBy: namaless $
 * @lastmodified	$Date: 2008-08-29 23:58:28 +0200 (ven, 29 ago 2008) $
 * @license			http://www.opensource.org/licenses/gpl-license.php GNU Public License
 */

/**
* Classe principale.
*/
class Lang
{
	/**
	 * Lingua corrente.
	 *
	 * @var string
	 **/
	var $_current;

	/**
	 * Contiene le linee della lingua.
	 *
	 * @var array
	 **/
	var $_lines;
	
	/**
	 * Lista delle lingue disponibili.
	 *
	 * @var array
	 **/
	var $_avariables;
	
	/**
	 * Lista delle stringhe locali.
	 *
	 * @var array
	 **/
	var $_locales = array(	'en'	=> "en_US",
							'it'	=> "it_IT");

	/**
	 * 
	 */
	protected static $_path = "languages/";

	/**
	 * Singleton Pattern.
	 *
	 * @var object
	 **/
	private static $instance = NULL;

	// --------------------------------------------------------------------

	/**
	 * Implemento il Singleton.
	 *
	 * @return	object
	 */
	public static function get_instance()
	{
		if ( self::$instance == NULL )
		{   
			$c = __CLASS__;
		    self::$instance =& new $c;
		}
		
		return self::$instance;
	}

	// --------------------------------------------------------------------

	private function __construct(){}
	
	// --------------------------------------------------------------------

	private function __clone(){}
	
	// --------------------------------------------------------------------

	public function __destruct(){}
	
	// --------------------------------------------------------------------

	/**
	 * Implemento il Factory.
	 */
	public static function factory()
	{
		// Inserisco un marcatore.
		Benchmark::mark('language_factory_start');
		
		// Imposta la lingua corrente.
		self::get_instance()->_set_current();
		
		// Carico il file degli errori.
		self::load('Errors');
		
		// Inserisco un marcatore.
		Benchmark::mark('language_factory_end');
	}

	// --------------------------------------------------------------------

	/**
	 * Ritorna la lingua corrente.
	 *
	 * @return string
	 */
	public static function get_current()
	{
		return self::get_instance()->_current;
	}
	
	// --------------------------------------------------------------------

	/**
	 * Imposta la lingua corrente.
	 *
	 */
	private function _set_current()
	{
		// Catturo la lingua del Browser.
		$browser_lang = env('HTTP_ACCEPT_LANGUAGE');
		$browser_lang = explode(",", $browser_lang);
		$browser_lang = $browser_lang[0];

		// Converto "-" in "_"
		$browser_lang = str_replace("-", "_", $browser_lang);
		
		// Controllo il formato della lingua.
		if ( strlen($browser_lang) < 5 )
		{
			// Catturo il formato locale della lingua.
			if ( isset($this->_locales[$browser_lang]) )
			{
				$browser_lang = $this->_locales[$browser_lang];
			}
		}
		
		$this->_current = $browser_lang;
	}
	
	// --------------------------------------------------------------------
	
	public static function load($lang_file)
	{
		// Creo l'oggetto.
		$lang = self::get_instance();
		
		// Definisco le paths.
		$langs_path = base_path(self::$_path . $lang->_current . "/{$lang_file}.xml");

		// Controllo se esiste il file
		if ( ! file_exists($langs_path) )
		{
			show_error_404(__("Language file '{1}' not found.", $lang_file), $langs_path);
		}
		else
		{
			// Creo l'oggetto XML.
			$xml = new SimpleXMLElement($langs_path, NULL, TRUE);

			// Controllo i files caricati.
			if ( count($xml) > 0 )
			{
				// Inserisco le linee nella classe.
				foreach ( $xml->phrase AS $phrase )
				{
					$lang->_lines[(string)($phrase->id)] = $phrase->value;
				}
			}
		}
	}

	// --------------------------------------------------------------------

	public static function line($line, $args = array())
	{
		// Creo l'oggetto.
		$lang = self::get_instance();
		
		$phrase = ( isset($lang->_lines[$line]) ? $lang->_lines[$line] : $line );
		
		foreach ( $args AS $key => $arg )
		{
			$phrase = str_replace('{' . $key . '}', $args[$key], $phrase);
		}

		return $phrase;
	}

}